package com.xktpx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xktpx.common.utils.PageUtils;
import com.xktpx.dao.BuyOrderDao;
import com.xktpx.entity.BuyOrderEntity;
import com.xktpx.entity.OrderGoodsDetailEntity;
import com.xktpx.entity.OrderLogisticsEntity;
import com.xktpx.entity.model.BuyOrder;
import com.xktpx.entity.model.OfferPurchaseDto;
import com.xktpx.entity.model.OrderGoods;
import com.xktpx.entity.model.SaleGoods;
import com.xktpx.service.BuyOrderService;
import com.xktpx.service.OrderGoodsDetailService;
import com.xktpx.service.OrderLogisticsService;
import com.xktpx.utils.CollectionUtil;
import com.xktpx.utils.CommonUtils;
import com.xktpx.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service("buyOrderService")
public class BuyOrderServiceImpl extends ServiceImpl<BuyOrderDao, BuyOrderEntity> implements BuyOrderService {

    @Autowired
    private OrderGoodsDetailService orderGoodsDetailService;
    @Autowired
    private OrderLogisticsService orderLogisticsService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<BuyOrderEntity> page = this.selectPage(
                new Query<BuyOrderEntity>(params).getPage(),
                new EntityWrapper<BuyOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Page<OrderGoods> queryListByGoodsId(Integer goodsId, Integer buyType, Integer sizeId, Integer pageNo, Integer pageSize) {
//        PageHelper.startPage(pageNo, pageSize);

        Page<OrderGoods> page = new Page<>(pageNo, pageSize);
//        Map<String, Object> map = new HashMap<>();
//        map.put("goodsId", goodsId);
//        map.put("buyType", buyType);
//        map.put("sizeId", sizeId);
//        page.setCondition(map);

        return page.setRecords(this.baseMapper.queryListByGoodsId(page, goodsId, buyType, sizeId));

    }

    @Override
    public List<SaleGoods> querySaleGoodsAmountOrderBySize(Integer goodsId) {
        List<SaleGoods> saleGoods = baseMapper.querySaleGoodsAmountOrderBySize(goodsId);
        if (saleGoods != null && !saleGoods.isEmpty()) {
            for (SaleGoods saleGoods1 : saleGoods) {
                SaleGoods saleGoods2 = baseMapper.queryBuyOrderAmountByGoodsSize(goodsId, saleGoods1.getSizeId());
                saleGoods1.setAmountMin(saleGoods2.getAmountTotal());
            }
            return saleGoods;
        }else{
            return Collections.EMPTY_LIST;
        }
    }

    @Override
    public List<SaleGoods> queryBuyGoodsAmountOrderBySize(Integer goodsId) {
        List<SaleGoods> saleGoods = baseMapper.queryBuyGoodsAmountOrderBySize(goodsId);
        if (CollectionUtils.isEmpty(saleGoods)){
            return Collections.EMPTY_LIST;
        }
        return saleGoods;

    }

    @Override
    public SaleGoods queryBuyOrderAmountByGoodsSize(Integer goodsId, Integer sizeId) {

        return baseMapper.queryBuyOrderAmountByGoodsSize(goodsId, sizeId);

    }

    @Override
    public List<OfferPurchaseDto> offerPurchaseList(Integer goodsId) {
        List<OfferPurchaseDto> offerPurchaseDtos = baseMapper.offerPurchaseList(goodsId);
        if (CollectionUtils.isEmpty(offerPurchaseDtos)){
            return Collections.EMPTY_LIST;
        }
        //最低售价
        List<SaleGoods> saleGoods = baseMapper.queryBuyGoodsAmountOrderBySize(goodsId);
        if (!CollectionUtils.isEmpty(saleGoods)){
            Map<Integer,SaleGoods> saleGoodsMap = CollectionUtil.listToMap(saleGoods,SaleGoods::getSizeId);
            offerPurchaseDtos.forEach(offerPurchaseDto -> {
                SaleGoods saleGoods1 = saleGoodsMap.get(offerPurchaseDto.getSizeId());
                if (saleGoods1!=null){
                    offerPurchaseDto.setMinAmountTotal(saleGoods1.getAmountTotal());
                }
            });
        }
        return offerPurchaseDtos;
    }

    @Override
    public void buyerCreateOrder(BuyOrder buyOrder) {
        BuyOrderEntity buyOrderEntity = new BuyOrderEntity();
        BeanUtils.copyProperties(buyOrder,buyOrderEntity);
        buyOrderEntity.setOrderNo(CommonUtils.getOrderIdByTime());
        buyOrderEntity.setCreateTime(new Date());
        buyOrderEntity.setBuyUserId(buyOrder.getUserId());
        buyOrderEntity.setOrderStatus(1);
        buyOrderEntity.setBuyType(1);
        //TODO 计算实际订单价格
        this.insert(buyOrderEntity);
        //订单商品详情
        OrderGoodsDetailEntity orderGoodsDetailEntity = new OrderGoodsDetailEntity();
        orderGoodsDetailEntity.setCreatedTime(new Date());
        orderGoodsDetailEntity.setDesc(buyOrder.getDesc());
        orderGoodsDetailEntity.setGoodsId(buyOrder.getGoodsId().longValue());
        orderGoodsDetailEntity.setGoodsName(buyOrder.getGoodsName());
        orderGoodsDetailEntity.setGoodsPrice(buyOrder.getAmountTotal());
        orderGoodsDetailEntity.setOrderId(buyOrderEntity.getId());
        orderGoodsDetailEntity.setOrderType(2);
        orderGoodsDetailService.insert(orderGoodsDetailEntity);
        //订单物流信息
        OrderLogisticsEntity orderLogisticsEntity = new OrderLogisticsEntity();
        orderLogisticsEntity.setOrderId(buyOrderEntity.getId());
        orderLogisticsEntity.setConsigneeAddress(buyOrder.getConsigneeAddress());
        orderLogisticsEntity.setConsigneeRealname(buyOrder.getConsigneeRealname());
        orderLogisticsEntity.setConsigneeTelphone(buyOrder.getConsigneeTelphone());
        orderLogisticsEntity.setOrderType(2);
        orderLogisticsService.insert(orderLogisticsEntity);
    }

    @Override
    public void offerPurchaseCreateOrder(BuyOrder buyOrder) {
        BuyOrderEntity buyOrderEntity = new BuyOrderEntity();
        BeanUtils.copyProperties(buyOrder,buyOrderEntity);
        buyOrderEntity.setOrderNo(CommonUtils.getOrderIdByTime());
        buyOrderEntity.setCreateTime(new Date());
        buyOrderEntity.setBuyUserId(buyOrder.getUserId());
        buyOrderEntity.setOrderStatus(-1);
        buyOrderEntity.setBuyType(2);
        //TODO 计算实际订单价格
        this.insert(buyOrderEntity);
        //订单商品详情
        OrderGoodsDetailEntity orderGoodsDetailEntity = new OrderGoodsDetailEntity();
        orderGoodsDetailEntity.setCreatedTime(new Date());
        orderGoodsDetailEntity.setDesc(buyOrder.getDesc());
        orderGoodsDetailEntity.setGoodsId(buyOrder.getGoodsId().longValue());
        orderGoodsDetailEntity.setGoodsName(buyOrder.getGoodsName());
        orderGoodsDetailEntity.setGoodsPrice(buyOrder.getAmountTotal());
        orderGoodsDetailEntity.setOrderId(buyOrderEntity.getId());
        orderGoodsDetailEntity.setOrderType(2);
        orderGoodsDetailService.insert(orderGoodsDetailEntity);
        //订单物流信息
        OrderLogisticsEntity orderLogisticsEntity = new OrderLogisticsEntity();
        orderLogisticsEntity.setOrderId(buyOrderEntity.getId());
        orderLogisticsEntity.setConsigneeAddress(buyOrder.getConsigneeAddress());
        orderLogisticsEntity.setConsigneeRealname(buyOrder.getConsigneeRealname());
        orderLogisticsEntity.setConsigneeTelphone(buyOrder.getConsigneeTelphone());
        orderLogisticsEntity.setOrderType(2);
        orderLogisticsService.insert(orderLogisticsEntity);
    }

    @Override
    public BuyOrderEntity selectlatestBuyOrder(Integer goodsId) {
        return baseMapper.selectLatestBuyOrder(goodsId);
    }

}
